"use strict";
/**
 * @name catShop
 * 猫币兑换商店
 */
const CATCOIN = 60100999
const BLUESTAR = 60100500
/**
 * @name blueStarShop
 * @param int id 商品ID
 * 用猫币购买蓝星
 */
exports.blueStarShop = async function () {
    let shopID = this.get('id', 'int');
    if (!shopID) {
        return this.error('args empty');
    }
    let shopConfig = this.config('blueStarShop', shopID);
    if (!shopConfig) {
        return this.error('config_not_exist', 'blueStarShop', shopID);
    }
    this.updater.item.sub(CATCOIN, shopConfig['shopCost']);
    // this.updater.item.add(BLUESTAR, shopConfig['shopAward']);
    //根据表里的shopBuytimes字段判断是否有购买次数限制
    if (shopConfig["shopBuytimes"] != 0) {
        //daily里获取blueShop商品的购买次数
        this.updater.daily.key("blueShop." + shopID);
        await this.updater.data();
        let buyTimes = this.updater.daily.get("blueShop." + shopID);
        if (buyTimes >= shopConfig["shopBuytimes"]) {
            return this.error('shop_num_limit', shopID);
        }
    }
    let flag = 0;
    //判断是否拥有双倍资格
    if (shopConfig["shopDouble"] == 1) {
        //根据role里的blueStarDouble字段，和base表里的blueStarShopResetDate
        //转成时间戳比较
        this.updater.role.key("blueStarDouble");
        await this.updater.data();
        var lastTime = this.updater.role.get("blueStarDouble." + shopID);
        //转成时间戳
        if (lastTime < new Date(this.config("base", "blueStarShopResetDate1").strVal).getTime()) {
            // this.updater.item.add(BLUESTAR, shopConfig['shopSpaeard']);
            flag = 1;
            this.updater.item.add(BLUESTAR, shopConfig['shopAward'] * 2);
            //获得双倍奖励，更新role里的blueStarDouble字段
            this.updater.role.set("blueStarDouble." + shopID, this.updater.time);
        } else if (lastTime > new Date(this.config("base", "blueStarShopResetDate2").strVal).getTime()) {
            this.updater.item.add(BLUESTAR, shopConfig['shopAward'] * 2);
            flag = 1;
            //获得双倍奖励，更新role里的blueStarDouble字段
            this.updater.role.set("blueStarDouble." + shopID, this.updater.time);
        }
    }
    if (!flag) {
        this.updater.item.add(BLUESTAR, shopConfig['shopAward'] + shopConfig['shopSpaeard']);
    }
    // this.updater.item.add(BLUESTAR, shopConfig['shopSpaeard']);

    this.updater.daily.add("record.43", 1);
    this.updater.record.add(43, 1);
    this.updater.daily.add("blueShop." + shopID, 1);
    return this.updater.save();
}



/**
 * @name giftShop
 * @param int id 商品ID
 * 礼包商店
 */
exports.giftShop = async function () {
    let shopID = this.get('id', 'int');
    if (!shopID) {
        return this.error('args empty');
    }
    let shopConfig = this.config('giftShop', shopID);
    if (!shopConfig) {
        return this.error('config_not_exist', 'giftShop', shopID);
    }
    this.updater.item.sub(shopConfig['shopCost'], shopConfig['shopPrice']);
    this.updater.item.add(shopConfig['shopAward'], 1);
    //通过表里的shopStart和shopStop字段判断是否在活动时间内,shopStop为0时代表没有结束时间，shopStart为0则报错
    if (shopConfig["shopStart"] != 0) {
        if (shopConfig["shopStop"] != 0) {
            if (this.updater.time < new Date(shopConfig["shopStart"]).getTime() || this.updater.time > new Date(shopConfig["shopStop"]).getTime()) {
                return this.error('shop_not_open', shopID);
            }
        } else {
            if (this.updater.time < new Date(shopConfig["shopStart"]).getTime) {
                return this.error('shop_not_open', shopID);
            }
        }
    }
    //根据表里的shopBuytimes字段判断是否有购买次数限制
    if (shopConfig["shopBuytimes"] != 0) {
        switch (shopConfig["shopTime"]) {
            case 0:
                this.updater.record.key("giftShop." + shopID);
                await this.updater.data();
                var buyTimes = this.updater.record.get("giftShop." + shopID);
                if (buyTimes >= shopConfig["shopBuytimes"]) {
                    return this.error('shop_num_limit', shopID);
                }
                this.updater.record.add("giftShop." + shopID, 1);
                break;
            case 1:
                this.updater.daily.key("giftShop." + shopID);
                await this.updater.data();
                var buyTimes = this.updater.daily.get("giftShop." + shopID);
                if (buyTimes >= shopConfig["shopBuytimes"]) {
                    return this.error('shop_num_limit', shopID);
                }
                this.updater.daily.add("giftShop." + shopID, 1);
                break;
            case 2:
                // this.updater.weekly.key("giftShop." + shopID);
                // let dkey = "eggPool." + config.Coll[i].poolId + "." + config.Coll[i].groupId;
                var buyTimes = await this.updater.daily.model.week("giftShop." + shopID);
                if (buyTimes["giftShop." + shopID] >= shopConfig["shopBuytimes"]) {
                    return this.error('shop_num_limit', shopID);
                }
                this.updater.daily.add("giftShop." + shopID, 1);
                break;
            default:
                return this.error('shop_config_error', shopID);
        }
    }
    this.updater.daily.add("record.43", 1);
    this.updater.record.add(43, 1);
    return this.updater.save();
}
